package mvc.core.config;

import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import mvc.core.log.Logger;

/**
 * 配置文件上下文
 * 
 * @author liuzy
 * @version 2015年5月17日
 */
public class ConfigContext {

	private static Map<String, String> configMap = new HashMap<>();

	public static void loadProperties(String... fileNames) {
		for (String fileName : fileNames) {
			loadProperties(fileName);
		}
		Logger.level = ConfigContext.get("log.level") != null ? ConfigContext.get("log.level").toUpperCase() : Logger.level;
	}
	
	private static void loadProperties(String fileName) {
		InputStream in = null;
		try {
			in = ConfigContext.class.getClassLoader().getResourceAsStream(fileName);
			Properties p = new Properties();
			p.load(in);
			Enumeration<?> names = p.propertyNames();
			while (names.hasMoreElements()) {
				String key = (String) names.nextElement();
				String value = p.getProperty(key);
				configMap.put(key.toLowerCase(), value);
			}
			p.clear();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null)
				try {
					in.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
	
	public static String get(String key) {
		return configMap.get(key);
	}
}
